<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
$return['diy']=$this->plugin_setting('diy');
$basic=$this->plugin_setting('basic');
$return['banner']=$basic['image'];
$return['basic']=$basic;
if($_RQ['op']=='main'){
	if(!$_RQ['id']){
		exi('没有找到该项目，请重新选择！','error','jy_vote/index/main');
	}
	if($_RQ['isdownsao']){
		$this->session('isdownsao',$_RQ['isdownsao']);
	}
	$this->session('rid',intval($_RQ['id']));
	//按点赞数排序作品
	$reply=pdo_get('jy_vote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if($reply['starttime']<time() && $reply['endtime']>time()){
				$reply['status']=1;
	}elseif($reply['endtime']<time()){
				$reply['status']=2;
	}else{
				$reply['status']=3;
	}
	$reply['type']=pdo_getcolumn('jy_vote_groups',array('uniacid'=>$_SESSION['uniacid'],'id'=>$reply['groupid']),'type');
	$alllist=pdo_getall('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'rid'=>$_RQ['id'],'status'=>1,'isshow'=>1),array('id','rid','title','likes','baselikenums','basevotenums'));
	$sum=count($alllist);
	$return['look']=pdo_getsum('jy_vote_opus',array('rid'=>$_RQ['id'],'status'=>1,'isshow'=>1),'look');
	if($alllist){
		$nums=0;
		foreach($alllist as &$item){
			$item['votenums']=pdo_count('jy_vote_votelog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			$jifen=pdo_getsum('jy_vote_sendlplog',array('oid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1),'jifen');
			if($reply['type']==1){
				$item['nums'] = $item['baselikenums']+$item['likes']+$jifen;
				$nums +=$item['nums'];
			}else{
				$item['nums'] = $item['basevotenums']+$item['votenums']+$jifen;
				$nums +=$item['nums'];
			}
		}
	}
	$return['alllist']=$alllist;
	$return['rid']=$_RQ['id'];
	set_html('title',$reply['title']);
	$return['sum']=$sum;
	$return['nums']=$nums;
	$reply['downtime']=$this->model()->date($reply['endtime']-TIMESTAMP);
	$reply['onlinetime']=json_decode($reply['onlinetime'],true);
	if($reply['is_linetime']){
		$day=date('Y-m-d',time());
		$start=strtotime($day.' '.$reply['onlinetime']['start']);
		$end=strtotime($day.' '.$reply['onlinetime']['end']);
		if($end<time()){
			$return['isout_onlinetime']=1;
		}
		if($start>time()){
			$return['isout_onlinetime']=1;
		}
		$return['novotetitle']='当前时间段不能投票,投票时间段：'.$reply['onlinetime']['start'].'~'.$reply['onlinetime']['end'];
	}
	$daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$oids=pdo_getall('jy_vote_opus',array('rid'=>$reply['id'],'status'=>1),'id','id');
	if($oids){
		$oids=array_keys($oids);
	}else{
		$oids=-1;
	}
	$isdairyvote=pdo_count('jy_vote_votelog',array('oid'=>$oids,'uniacid'=>$_SESSION['uniacid'],'createtime >'=>$daytime));
	$reply['lastdairyvote']=$reply['dairyvote']-$isdairyvote;
	$return['reply']=$reply;
	$return['setting']=array(
		'hdps'=>json_decode($reply['hdps'],true),
		'bankuai1'=>json_decode($reply['bankuai'],true),
	);
	$return['select']=array();
	$return['intDiff']=$reply['endtime']- time();
	$return['endstatus']= $reply['status'];
	$return['apstatus']= $reply['apendtime'] < time()? 2 : $reply['apstarttime'] <time()?1:0;
	$return['sharedata']=array(
		'title'=>$reply['title'],
		'desc'=>$reply['infomsg'],
		'link'=>custom_url('jy_vote/r_detail//id='.intval($_RQ['id']),true),
		'imgUrl'=>tomedia($reply['thumb']),
	);
	$return['params']=array();
	exi($return);
}
if($_RQ['op']=='setvote'){
	$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','groupid','avatar','nickname'));
	$postdata=get_postdata();
	$rid=intval($_RQ['rid']);
	foreach($postdata as $k=>$v){
		if($v!=1){
			continue;
		}
		$oid=intval($k);
		if(!$oid){
			exi('未正确获取作品id','error');
		}
		$opus = pdo_get('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'id'=>$oid));
		$reply = pdo_get('jy_vote_reply',array('uniacid'=>$_SESSION['uniacid'],'id'=>$opus['rid']));
		if($reply['is_yanzheng'] && !check_image_code(trim($postdata['code']))){
			exi('验证码错误','error');
		}
		if($reply['endtime']<time()){
				exi('该活动已结束！','error');
		}
		if($reply['starttime']>time()){
				exi('该活动还没开始！','error');
		}
		//投票最少作品数
		$opus_sum=pdo_count('jy_vote_opus',array('uniacid'=>$_SESSION['uniacid'],'rid'=>$opus['rid']));
		if($opus_sum<$reply['minpeople']){
			exi('作品数量未达到投票最小作品数，暂无法投票','error');
		}
		//用户已投票数量(包含未审核)
		$vote_sum=pdo_count('jy_vote_votelog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'oid'=>$oid));
		if($vote_sum>=$reply['everyonevote'] && $reply['everyonevote']>0){
			exi('每人最多投'.$reply['everyonevote'].'票，您已投完！','error');
		}
		//日投票
		//获得当日凌晨的时间戳
		$today = strtotime(date("Y-m-d"),time());
		$end = $today+60*60*24;
		$dayvote_sum=pdo_count('jy_vote_votelog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$today));
		if($dayvote_sum>=$reply['dairyvote'] && $reply['dairyvote']>0){
			exi('每人每日最多投'.$reply['dairyvote'].'票，您已投完！','error');
		}
		//每日给同一作品投票
		$dayuservote_sum=pdo_count('jy_vote_votelog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'createtime >'=>$today,'oid'=>$oid));
		if($dayuservote_sum>=$reply['everyoneuser'] && $reply['everyoneuser']>0){
			exi('同一作品每人每日最多投'.$reply['everyoneuser'].'票，您已投完！','error');
		}
		$isdownsao=$this->session('isdownsao');
		$data=array(
			'mid'=>$_SESSION['uid'],
			'oid'=>$oid,
			'uniacid'=>$_SESSION['uniacid'],
			'is_shenhe'=>1,
			'isline'=>$isdownsao?1:0,
			'user_ip'=>CLIENT_IP,
			'createtime'=>time(),
		);
		pdo_insert('jy_vote_votelog',$data);
		$return['logid']=pdo_insertid();
		//积分奖励：
		//投票赠送
		if($reply['vote_type']>0 && $reply['votezs']){
			cfc('core')->member_credit($_SESSION['uid'],'credit'.$reply['vote_type'],$reply['votezs'],1,'jy_vote','投票赠送');
		}
		if($reply['vote_type']>1 && $reply['vote_couponid']){
			$this->model('coupon')->getcoupon($reply['vote_couponid']);
		}
	}
	$rand=rand(1,100) ;
	if($rand>$reply['tanchu_rate']){
		$istanchu=0;
	}else{
		$istanchu=1;
	}
	if($reply['vote_codetype']){
		if($reply['vote_codetype']==1 && $isdownsao){
			$return['getcode']=1;
		}
		if($reply['vote_codetype']==2 && !$isdownsao){
			$return['getcode']=1;
		}
		if($reply['vote_codetype']==3){
			$return['getcode']=1;
		}
	}
	$return['istanchu']=$reply['hb_draw']?$istanchu:0;
	exi($return);
}